<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<form name="myform">
    用户名：<input type="text" name="username">
    密码：<input type="password" name="pwd">
    <input type="button" name="sub" value="登录" onclick="login()">
    <a href="javascript:del('username')">清除记录</a>
</form>
<script type="text/javascript">
    function login(){
        var uname=myform.username.value;
        var pwd=myform.pwd.value;
        if(uname.length==0){
            alert("用户名不可为空");
            return false;
        }else if(uname!="mr"){
            alert("用户名不正确");
            return false;
        }else if(pwd.length==0){
            alert("密码不可为空");
            return false;
        }else if(pwd!="111"){
            alert("密码不正确");
            return false;
        }else{
            alert("登录成功");
            writeCookie("username",uname);
        }
    }
    function writeCookie(name,value){
        document.cookie=encodeURI(name+"="+value);
    }
    function readCookie(name){
        var cookieString=decodeURI(document.cookie);
        var cookieArray=cookieString.split("; ");
        for(var i=0;i<cookieArray.length;i++){
            var cookieNum=cookieArray[i].split("=");
            if(cookieNum[0]==name) return cookieNum[1];
        }
        return "";
    }
    myform.username.value=readCookie('username');
    function del(name){
        var date=new Date();
        date.setTime(date.getTime()-1);
        document.cookie=name+"=清除; expires="+date.toGMTString();
        myform.username.value="";
    }
</script>
</body>
</html>